<!--
 * @Description:: 请补充填写
 * @Version: 1.0
 * @Author: 小白
 * @Date: 2025-09-19 14:03:58
 * @LastEditors: 小白
 * @LastEditTime: 2025-09-19 14:25:57
 * @FilePath: \tmsadmin\src\views\home\components\chaifenModal.vue
-->
<template>
  <el-dialog
    v-model="dialogVisible"
    :title="title"
    width="500px"
    :append-to-body="true"
    :before-close="handleClose">
    <el-form ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-width="120px"
      class="demo-ruleForm"
      status-icon>
      <el-form-item
        label="拆分数量"
        prop="splitNum"
       >
        <el-input
          placeholder="请输入"
          type="number"
          @input="handleInput"
          v-model="ruleForm.splitNum" />
      </el-form-item>
      
    </el-form>
    <template #footer>
      <span
        class="dialog-footer">
        <el-button
          @click="handleCloses(ruleFormRef)">取消</el-button>
        <el-button
          type="primary"
          @click="submit(ruleFormRef)">
          确 认
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import {ref } from 'vue'
import { ElMessage, FormInstance } from 'element-plus'
import { REQUEST } from '@/api/request'
const ruleFormRef = ref<FormInstance>()
const title = ref('')
const dialogVisible = ref(false)
const emits = defineEmits(['ok'])
const ruleForm = ref({
  id: '',
  splitNum: '',
})
const handleInput = (val: any) => {
  if(val >= ruleForm.value.shippingQuantity){
    ElMessage.error('拆分数量不能大于发运数量')
    ruleForm.value.splitNum = ''
  }
}
const rules = ref({
  splitNum: [{ required: true, message: '请输入',trigger: 'change' }],
})
const openModal = (t: any) => {
  console.log(t.row)
      ruleForm.value = {
        id:'',
        splitNum: '',
      }
      dialogVisible.value = true
      title.value = t.title
      ruleForm.value = t.row
    }
    const handleClose = () => {
      dialogVisible.value = false
      emits('ok')
    }
    const handleCloses = (formEl: FormInstance | undefined) => {
      if (!formEl) return
      formEl.resetFields()
      emits('ok')
      dialogVisible.value = false
    }
    const submit = async (formEl: FormInstance | undefined) => {
      if (!formEl) return
      await formEl.validate((valid, fields) => {
        if (valid) {
          REQUEST.POST('ORDERCHAIFEN', {
            splitNum: ruleForm.value.splitNum,
            id: ruleForm.value.id
          }).then((res: any) => {
            emits('ok')
            dialogVisible.value = false
            ElMessage.success('拆分成功!')
          })
        }
      })
    }
    defineExpose({ openModal })
</script>

<style lang="" scoped></style>
